package com.sba.core.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sba.core.domain.Region;
import com.sba.core.service.RegionService;
import com.sba.core.util.StringUtil;
import com.sba.core.web.vo.ZTree;

/**
 * 
 * @author feiliming
 *
 */
@Controller
@RequestMapping("${ssh.admin-path}/region")
public class RegionController extends BaseController {

	private final RegionService regionService;
	
	@Autowired
	public RegionController(RegionService regionService){
		this.regionService = regionService;
	}
	
	@GetMapping
	public String index(Map<String, Object> model){
		return adminThemePath + "/sys/region";
	}
	
	@RequestMapping(path = "/list", method = RequestMethod.GET)
	public @ResponseBody JSONObject getRegions(Long parentId, String regionType, String name, Integer limit, Integer offset){
		if(limit == null){
			limit = Integer.MAX_VALUE;
		}
		if(offset == null){
			offset = 0;
		}
		
		Page<Region> page;
		//如果parentId和regionType都传, 则只parentId有效
		if(parentId != null){
			if(StringUtil.isNotBlank(name)){
				page = regionService.getRegionsByparentIdAndNameLike(parentId, name, limit, offset);
			}else{
				page = regionService.getRegionsByParentId(parentId, limit, offset);
			}
		}else if(StringUtil.isNotBlank(regionType)){
			if(StringUtil.isNotBlank(name)){
				page = regionService.getRegionsByRegionTypeAndNameLike(regionType, name, limit, offset);
			}else{
				page = regionService.getRegionsByRegionType(regionType, limit, offset);
			}
		}else{
			if(StringUtil.isNotBlank(name)){
				/**try {
					String dname = URLDecoder.decode(name, "UTF-8");
					name = dname;
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}*/
				page = regionService.getRegionsByNameLike(name, limit, offset);
			}else{
				page = regionService.getRegions(limit, offset);
			}
		}
		
		JSONObject obj = new JSONObject();
		obj.put("list", page.getContent());
		obj.put("total", page.getTotalElements());
		obj.put("totalPages", page.getTotalPages());
		return obj;
	}
	
	@RequestMapping(path = "/ztlist", method = RequestMethod.GET)
	public @ResponseBody List<ZTree> getRegionsForZTree(Long parentId, String regionType, String name, Integer limit, Integer offset){
		if(limit == null){
			limit = Integer.MAX_VALUE;
		}
		if(offset == null){
			offset = 0;
		}
		
		Page<Region> regions;
		//如果parentId和regionType都传, 则只parentId有效
		if(parentId != null){
			if(StringUtil.isNotBlank(name)){
				regions = regionService.getRegionsByparentIdAndNameLike(parentId, name, limit, offset);
			}else{
				regions = regionService.getRegionsByParentId(parentId, limit, offset);
			}
		}else if(StringUtil.isNotBlank(regionType)){
			if(StringUtil.isNotBlank(name)){
				regions = regionService.getRegionsByRegionTypeAndNameLike(regionType, name, limit, offset);
			}else{
				regions = regionService.getRegionsByRegionType(regionType, limit, offset);
			}
		}else{
			if(StringUtil.isNotBlank(name)){
				regions = regionService.getRegionsByNameLike(name, limit, offset);
			}else{
				regions = regionService.getRegions(limit, offset);
			}
		}
		
		List<ZTree> ztRegions = new ArrayList<ZTree>();
		for(Region r : regions.getContent()) {
			ZTree zt = new ZTree();
			zt.setId(r.getId());
			zt.setpId(r.getParentId());
			zt.setName(r.getName());
			ztRegions.add(zt);
		}
		return ztRegions;
	}
	
	@PostMapping("/save")
	public @ResponseBody JSONObject saveRegion(Region region){
		region.setCreateTime(new Date());
		region.setIsDelete(0);
		JSONObject obj = new JSONObject();
		try {
			regionService.saveOrUpdate(region);
			obj.put("success", true);
			obj.put("msg", "保存地区成功！");
		} catch (Exception e) {
			obj.put("success", false);
			obj.put("msg", "保存地区失败！");
		}
		return obj;
	}
	
	@GetMapping("/get")
	public @ResponseBody Region getRegion(Long id){
		return regionService.getById(id);
	}
	
	@RequestMapping(path = "/delete", method = RequestMethod.POST)
	public @ResponseBody JSONObject deleteRegion(Long id){
		Page<Region> rp = regionService.getRegionsByParentId(id, 1, 0);
		JSONObject obj = new JSONObject();
		if(rp.getTotalElements() > 0){
			obj.put("success", false);
			obj.put("msg", "该地区下有子节点，禁止删除！");
			return obj;
		}else{
			Region region = regionService.getById(id);
			region.setIsDelete(1);
			try {
				regionService.saveOrUpdate(region);
				obj.put("success", true);
				obj.put("msg", "删除地区成功！");
			} catch (Exception e) {
				obj.put("success", false);
				obj.put("msg", "删除地区失败！");
			}
			return obj;
		}
	}
	
}
